package com.${projectName}.${moduleName}.service.${packageName}.service.impl;

import com.hkbt.base.frame.service.HKServiceImpl;
import com.${projectName}.${moduleName}.service.${packageName}.entity.${tableName}Entity;
import com.${projectName}.${moduleName}.service.${packageName}.mapper.${tableName}Mapper;
import com.${projectName}.${moduleName}.service.${packageName}.service.${tableName}Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hkbt.base.common.resultCode.CommonResultCode;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkbt.base.frame.domain.bo.PageBO;
import com.hkbt.base.frame.domain.param.DefaultPageParam;
import com.hkbt.base.frame.util.AssertUtils;
import com.${projectName}.${moduleName}.param.${packageName}.${tableName}Param;
import com.${projectName}.${moduleName}.service.${packageName}.bo.${tableName}BO;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Service;

/**
 * ${comment}Service实现类
 * @author yanpanlong
 */
@Service
public class ${tableName}ServiceImpl extends HKServiceImpl<${tableName}Mapper, ${tableName}Entity> implements ${tableName}Service {


    @Override
    public PageBO<${tableName}BO> get${tableName}Page(DefaultPageParam<${tableName}Param> param) {
        LambdaQueryWrapper<${tableName}Entity> wrapper = buildWrapper(param.getData());
        Page<${tableName}Entity> page = super.page(new Page<>(param.getPageNo(), param.getPageSize()), wrapper);
        return super.toPageBo(page, ${tableName}BO.class);
    }

    @Override
    public List<${tableName}BO> get${tableName}List(${tableName}Param param) {
        LambdaQueryWrapper<${tableName}Entity> wrapper = buildWrapper(param);
        List<${tableName}Entity> entities = super.list(wrapper);
        if (CollectionUtils.isEmpty(entities)) {
            return Collections.EMPTY_LIST;
        }
        return super.toBOs(entities, ${tableName}BO.class);
    }

    @Override
    public ${tableName}BO get${tableName}ById(Long id) {
        AssertUtils.nonNullAny(id);
        ${tableName}Entity entity = super.getOne(super.lambdaWrapperSelect(${tableName}BO.class).eq(${tableName}Entity::getId, id));
        return super.toBO(entity, ${tableName}BO.class);
    }

    @Override
    public Long insert${tableName}(${tableName}Param param) {
    //        AssertUtils.nonNullAny(param.getName());
        ${tableName}Entity entity = super.toEntity(param);
        this.save(entity);
        return entity.getId();
    }

    @Override
    public Boolean update${tableName}(${tableName}Param param) {
        AssertUtils.nonNullAny(param.getId());
        ${tableName}Entity entity = this.getById(param.getId());
        AssertUtils.isTrue(Objects.nonNull(entity), CommonResultCode.EMPTY_DATA);
        return super.updateById(super.toEntity(param));
    }

    @Override
    public Boolean delete${tableName}(Long id) {
        AssertUtils.nonNullAny(id);
        ${tableName}Entity oldEntity = this.getById(id);
        AssertUtils.isTrue(Objects.nonNull(oldEntity), CommonResultCode.EMPTY_DATA);
        return super.removeById(id);
    }

    private LambdaQueryWrapper<${tableName}Entity> buildWrapper(${tableName}Param param){
        LambdaQueryWrapper<${tableName}Entity> wrapper = super.lambdaWrapperSelect(${tableName}BO.class);
        return wrapper;
    }


}
